#!/usr/bin/env python3

import numpy as np
import cv2 as cv

# cap = cv.VideoCapture("../video/test.mp4")
# cap = cv.VideoCapture("output.avi")
cap = cv.VideoCapture(0)

# 定义编解码器并创建VideoWriter对象
fourcc = cv.VideoWriter_fourcc(*'XVID')
out = cv.VideoWriter('output.avi', fourcc, 20.0, (640,  480))

while cap.isOpened():
    ret, frame = cap.read()
    if not ret:
        print("Can't receive frame (stream end?). Exiting ...")
        break
    frame = cv.flip(frame, 1)
    # 写翻转
    out.write(frame)  # 将帧写入视频
    cv.imshow('frame', frame)
    if cv.waitKey(1) & 0xFF == ord('q'):
        break
    
# 完成工作后释放所有内容
cap.release()
out.release()
cv.destroyAllWindows()
